using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;

namespace VISS.API.Users {
	class UserAvatarDA {

		private const string SP_GetAll = "dbo.UserAvatar_GetAll";
		private const string SP_GetByUserKey = "dbo.UserAvatar_GetByUserKey";
		private const string SP_Insert = "dbo.UserAvatar_Insert";
        private const string SP_Update = "dbo.UserAvatar_Update";
        private const string SP_UpdateStatus = "dbo.UserAvatar_UpdateStatus";

		public static UserAvatarEnt CreateFromReader(IDataReader reader) {
			UserAvatarEnt item = new UserAvatarEnt();
			item.UserKey = ConvertUtils.ToString(reader["UserKey"]);
			item.FileName = ConvertUtils.ToString(reader["FileName"]);
			item.ImageStoreID = ConvertUtils.ToInt(reader["ImageStoreID"]);
            item.Status = (EUserAvatarStatus)ConvertUtils.ToByte(reader["Status"]);
			item.DateCreated = ConvertUtils.ToDateTime(reader["DateCreated"]);
			item.DateModified = ConvertUtils.ToDateTime(reader["DateModified"]);
			return item;
		}

		public static List<UserAvatarEnt> GetAll(string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			List<UserAvatarEnt> result = new List<UserAvatarEnt>();
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_GetAll)) {
				using (IDataReader reader = db.ExecuteReader(cmd)) {
					while (reader.Read()) {
						result.Add(CreateFromReader(reader));
					}
				}
			}
			return result;
		}

        public static UserAvatarEnt GetByUserKey(string userKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			UserAvatarEnt result = null;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_GetByUserKey)) {
				db.AddParameter(cmd, "UserKey", userKey);
				using (IDataReader reader = db.ExecuteReader(cmd)) {
					if (reader.Read()) {
						result = CreateFromReader(reader);
					}
				}
			}
			return result;
		}

        public static int Insert(UserAvatarEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Insert)) {
				db.AddParameter(cmd, "UserKey", item.UserKey);
				db.AddParameter(cmd, "FileName", item.FileName);
				db.AddParameter(cmd, "ImageStoreID", item.ImageStoreID);
                db.AddParameter(cmd, "Status", ConvertUtils.ToBool((Byte)item.Status));
				db.AddParameter(cmd, "DateCreated", item.DateCreated);
				db.AddParameter(cmd, "DateModified", item.DateModified);

				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

        public static int Update(UserAvatarEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            int result = -1;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_Update)) {
                db.AddParameter(cmd, "UserKey", item.UserKey);
                db.AddParameter(cmd, "FileName", item.FileName);
                db.AddParameter(cmd, "Status", item.Status);
                db.AddParameter(cmd, "DateModified", item.DateModified);
                if (dbCon != null && dbTrans != null) {
                    result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
                }
                else {
                    result = db.ExecuteNonQuery(cmd);
                }
            }
            return result;
        }

        public static int UpdateStatus(UserAvatarEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_UpdateStatus)) {
				db.AddParameter(cmd, "UserKey", item.UserKey);
                db.AddParameter(cmd, "Status", ConvertUtils.ToBool((Byte)item.Status));
				db.AddParameter(cmd, "DateModified", item.DateModified);
				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}
	}
}